package com.panshi.controller.home;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.panshi.api.HomeApi;
import com.panshi.api.IMessagApi;
import com.panshi.api.IQuestionApi;
import com.panshi.api.UserGetApi;
import com.panshi.utils.GetSessionUser;
import com.panshi.vo.UserVO;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class HomeController {

    @Autowired
    HomeApi homeApi;

    @Autowired
    UserGetApi userGetApi;

    @Autowired
    IQuestionApi questionApi;


    @CrossOrigin(origins = "*", maxAge = 3600)
    @GetMapping(value = {"/home/{type}", "/home"})
    public String home(@PathVariable(required = false) String type, Map<String, Object> model) {
        if (null == type) {
            model.put("now", "1");
        } else {
            model.put("now", type);
        }
        Pages<List<Question>> p = new Pages<>();
        p.setStartPage(1);
        p.setPageSize(10);
        p.setState(1);
        p.setOrder(1);//按阅读次数排序
        DataResultQA<Pages<Question>> d2 = questionApi.inquirePagesQuestion(p);
        Pages<Question> p2 = d2.getData();
        List<Question> listQuestion = p2.getList();
        int userId = GetSessionUser.getSessionUserId();

        model.put("userId", userId);
        model.put("loginUser", userGetApi.findUserByUid(userId + ""));
        model.put("listQuestion", listQuestion);
        model.put("titlelist", homeApi.listTitleVOs());
        model.put("hotBot", homeApi.hotBot());
        model.put("newct", homeApi.listHomeNewArticleOkVOs());
        return "home_page/home";
    }

    //主页 加载更多
    @CrossOrigin(origins = "*", maxAge = 3600)
    @PostMapping("/home/load/{type}/{page}")
    public String index(@PathVariable(value = "type") Integer type, @PathVariable(value = "page") Integer page, Map<String, Object> model) {
        Pages<Question> pages = new Pages<>();
        pages.setStartPage(1);
        pages.setPageSize(10);
        pages.setState(1);
        pages.setOrder(1);//按阅读次数排序
        DataResultQA<Pages<Question>> d2 = questionApi.inquirePagesQuestion(pages);
        pages = d2.getData();
        model.put("pages", pages);
        model.put("zxList", homeApi.listHomeArticleOkVOs(type, page));
        model.put("qalist", homeApi.listTitleVOs());
        model.put("titlelist", homeApi.listTitleVOs());
        return "home_page/load";
    }

    //主页 加载更多
    @CrossOrigin(origins = "*", maxAge = 3600)
    @PostMapping("/home/loadqa/{type}/{page}")
    public String question(@PathVariable(value = "type") Integer type, @PathVariable(value = "page") Integer page, Map<String, Object> model) {
        Pages<Question> pages = new Pages<>();
        pages.setStartPage(1);
        pages.setPageSize(10);
        pages.setState(1);
        if (type == 2) {
            type = 0;
        } else if (type == 3) {
            type = 1;
        }
        pages.setOrder(type);//按阅读次数排序
        DataResultQA<Pages<Question>> d2 = questionApi.inquirePagesQuestion(pages);
        pages = d2.getData();
        model.put("pages", pages);
        return "home_page/questionLoad";
    }


    //主页 页数
    @CrossOrigin(origins = "*", maxAge = 3600)
    @GetMapping("/home/page")
    @ResponseBody
    public int countHomePage() {
        return homeApi.countHomePage();
    }
}
